using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Markup;
using Common;
using Common.Enum;
using device;
using SExperiment;
using SSettings;

namespace MainView.View
{
	public partial class UCBasicSetting : System.Windows.Controls.UserControl, IComponentConnector
	{
		public static readonly DependencyProperty IsDecryptProperty = DependencyProperty.Register("IsDecrypt", typeof(bool), typeof(UCBasicSetting), new UIPropertyMetadata(true));

		private Experiment _Experiment;

		private bool msgRunning;

		public bool IsDecrypt
		{
			get
			{
				return (bool)GetValue(IsDecryptProperty);
			}
			set
			{
				SetValue(IsDecryptProperty, value);
			}
		}

		public UCBasicSetting(Experiment experiment)
		{
			_Experiment = experiment;
			base.DataContext = _Experiment;
			InitializeComponent();
			cmbType.ItemsSource = EnumManagement.GetAllProjectType();
			cmbType.SelectedValue = _Experiment.Type;
			textEditOperator.ItemsSource = ConfigSettings.GetInstance().History.ExperimentOperatorList;
			textEditAuditor.ItemsSource = ConfigSettings.GetInstance().History.ExperimentAuditorList;
			_Experiment.Device.ModelChangedEvent += Device_ModelChangedEvent;
			textPath.IsEnabled = ConfigReader.GetInstance().GetFixExperimentPathFromIni() == 0 && IsDecrypt;
			textPathBrowser.IsEnabled = textPath.IsEnabled;
			RefreshDeviceInfo();
			RefreshFastMode();
			if (_Experiment.Device.Model == Device.DeviceModel_40P)
			{
				_Experiment.UseHotBonnet = false;
				checkHotBonnet.IsEnabled = false;
			}
		}

		private void RefreshFastMode()
		{
			bool flag = _Experiment.Device.Fast && _Experiment.Type == EProjectType.AQ;
			radioButtonTCFast.Visibility = ((!flag) ? Visibility.Collapsed : Visibility.Visible);
			gridModule.ColumnDefinitions[1].Width = new GridLength(flag ? 1 : 0, (!flag) ? GridUnitType.Pixel : GridUnitType.Star);
			gridTemperatureControl.ColumnDefinitions[2].Width = new GridLength(flag ? 1 : 0, (!flag) ? GridUnitType.Pixel : GridUnitType.Star);
			if (!flag && _Experiment.TemperatureControlMode == ETemperatureControlMode.Fast)
			{
				_Experiment.TemperatureControlMode = ETemperatureControlMode.Module;
			}
		}

		private void Device_ModelChangedEvent(object sender, EventArgs e)
		{
			RefreshDeviceInfo();
		}

		private void RefreshDeviceInfo()
		{
			if (string.IsNullOrEmpty(_Experiment.Device.DispalyModel))
			{
				tbModel.Text = Utility.GetMessage("virtual_device_run");
			}
			else
			{
				tbModel.Text = _Experiment.Device.DispalyModel;
			}
			if (string.IsNullOrEmpty(_Experiment.Device.Series))
			{
				tbSeries.Text = Utility.GetMessage("virtual_device_run");
				return;
			}
			string deviceRemark = ConfigReader.GetInstance().GetDeviceRemark(_Experiment.Device.Series);
			if (!string.IsNullOrEmpty(deviceRemark))
			{
				tbSeries.Text = _Experiment.Device.Series + "(" + deviceRemark + ")";
			}
			else
			{
				tbSeries.Text = _Experiment.Device.Series;
			}
		}

		private void cmbType_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			if (cmbType.SelectedValue != null)
			{
				bool flag = true;
				EProjectType type = (EProjectType)cmbType.SelectedValue;
				if (!_Experiment.CanTypeChange(type))
				{
					System.Windows.MessageBox.Show(string.Format(Utility.GetMessage("msg_experiment_type_change_must_AB"), EnumManagement.GetProjectTypeName(type)));
					flag = false;
				}
				else if (_Experiment.Wells.Where((Well s) => s.Project != null).Count() > 0 && System.Windows.MessageBox.Show(Utility.GetMessage("msg_experiment_type_change"), Utility.GetMessage("Warning"), MessageBoxButton.YesNo, MessageBoxImage.Exclamation, MessageBoxResult.No) == MessageBoxResult.No)
				{
					flag = false;
				}
				if (!flag)
				{
					cmbType.SelectionChanged -= cmbType_SelectionChanged;
					cmbType.SelectedValue = _Experiment.Type;
					cmbType.SelectionChanged += cmbType_SelectionChanged;
					e.Handled = true;
				}
				else
				{
					_Experiment.Type = type;
					ConfigSettings.GetInstance().MemorySettings.Type = _Experiment.Type;
					RefreshFastMode();
				}
			}
		}

		private void textEditName_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
		{
			if (!msgRunning)
			{
				msgRunning = true;
				if (string.IsNullOrEmpty(textEditName.Text))
				{
					System.Windows.MessageBox.Show(Utility.GetMessage("msg_filename_is_null"));
					textEditName.Text = _Experiment.Name;
					textEditName.SelectAll();
					e.Handled = true;
				}
				if (MainWindow.ExperimentList.Where((Experiment s) => s != _Experiment && s.Name == textEditName.Text).Count() > 0)
				{
					System.Windows.MessageBox.Show(string.Format(Utility.GetMessage("msg_filename_is_repeat"), textEditName.Text));
					textEditName.Text = _Experiment.Name;
					textEditName.SelectAll();
					e.Handled = true;
				}
				msgRunning = false;
			}
		}

		private void btnBrowse_Click(object sender, RoutedEventArgs e)
		{
			FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
			folderBrowserDialog.SelectedPath = textPath.Text;
			folderBrowserDialog.ShowNewFolderButton = true;
			if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
			{
				textPath.Text = folderBrowserDialog.SelectedPath;
			}
		}

		private void checkEditUseHotBonnet_SourceUpdated(object sender, DataTransferEventArgs e)
		{
			ConfigSettings.GetInstance().MemorySettings.UseHotBonnet = _Experiment.UseHotBonnet;
		}

		private void checkEditTemperatureControlMode_SourceUpdated(object sender, DataTransferEventArgs e)
		{
			ConfigSettings.GetInstance().MemorySettings.TemperatureControlMode = _Experiment.TemperatureControlMode;
		}

		private void checkEditScanMode_SourceUpdated(object sender, DataTransferEventArgs e)
		{
			ConfigSettings.GetInstance().MemorySettings.ScanMode = _Experiment.ScanMode;
		}
	}
}
